/*
Copyright (C) 2009-2010 Xanadu Development Team

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; version 2
of the License.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

#include "../tools/PacketCreator.h"
#include "Summons.h"
#include "Summon.h"
#include "Player.h"
#include "Map.h"

Summons::~Summons() {
	if(main)
		delete main;
	if(puppet)
		delete puppet;
}

void Summons::removeSummon(int skillid) {
	string name = getSummonName(skillid);
	player->send(PacketCreator().showMessage(name + " has run out of time and will disappear.", 5));
	if (main) {
		player->getMap()->send(PacketCreator().removeSummon(player->getID(), main->getObjectID(), true));
		main = 0;
		delete main;
	}
	if (puppet) {
		player->getMap()->send(PacketCreator().removeSummon(player->getID(), puppet->getObjectID(), true));
		puppet = 0;
		delete puppet;
	}
}

void Summons::addSummon(int skillid, int skillLvl, int hp) {
	if (skillid == 13111004 || skillid == 3111002 || skillid == 3211002) {
		if (puppet) {
			removeSummon(skillid);
		}
		puppet = new Summon(skillid, skillLvl);
		Position newPos;
		newPos = player->getPosition();
		if (player->getStance() == 4)
			newPos.x += 200;
		else
			newPos.x -= 200;
		puppet->setPosition(newPos);
		puppet->setFoothold(player->getFoothold());
		puppet->setStance(player->getStance());
		puppet->setHp(hp);
		player->getMap()->send(PacketCreator().showSummon(player->getID(), puppet, true));
	} else {
		if (main) {
			removeSummon(skillid);
		}
		main = new Summon(skillid, skillLvl);
		main->setPosition(player->getPosition());
		main->setFoothold(player->getFoothold());
		main->setStance(player->getStance());
		player->getMap()->send(PacketCreator().showSummon(player->getID(), main, true));
	}
}

bool Summons::isSummon(int skillid) {
	switch(skillid) {
			case 3211005: // Golden Eagle
			case 3111005: // Silver Hawk
			case 2311006: // Summon Dragon
			case 3221005: // Frostprey
			case 3121006: // Phoenix
			case 5211002: // Giovatia
			case 5211001: // Octopus
			case 5220002: // Wrath Of The Octopi
			case 2121005: // Elquines
			case 2221005: // Ifrit
			case 2321003: // Bahamut
			case 3111002: // Puppet ranger
			case 3211002: // Puppet sniper
			case 11001004: // Soul
			case 12001004: // Flame
			case 13001004: // Storm
			case 13111004: // Puppet
			case 14001005: // Darkness
			case 15001004: // Lightning
				return true; 
				break;
			default:
				return false;
				break;
	}
}

Summon* Summons::getSummonById(int id) {
	if(main && main->getObjectID() == id) {
		return main;
	}
	else if(puppet && puppet->getObjectID() == id) {
		return puppet;
	} else {
		return 0;
	}
}

string Summons::getSummonName(int summonid) {
	string ret = "Summon";
	switch (summonid) {
		case 2321003: ret = "Bahamut"; break;
		case 5211002: ret = "Gaviota"; break;
		case 5211001:
		case 5220002: ret = "Octopus"; break;
		case 3211002:
		case 3111002:
		case 13111004: ret = "Puppet"; break;
		case 2311006: ret = "Summon Dragon"; break;
		case 3111005: ret = "Silver Hawk"; break;
		case 3211005: ret = "Golden Eagle"; break;
		case 1321007: ret = "Beholder"; break;
		case 2121005: ret = "Elquines"; break;
		case 2221005: ret = "Ifrit"; break;
		case 3121006: ret = "Phoenix"; break;
		case 3221005: ret = "Frostprey"; break;
		case 11001004: ret = "Soul"; break;
		case 12111004: ret = "Ifrit"; break;
		case 12001004: ret = "Flame"; break;
		case 13001004: ret = "Storm"; break;
		case 14001005: ret = "Darkness"; break;
		case 15001004: ret = "Lightning"; break;
	}
	return ret;
}

void Summons::showSummons(Player* target, bool map) {
	if(target && !map) {
		if(main)
			target->send(PacketCreator().showSummon(player->getID(), main, false));
		if(puppet)
			target->send(PacketCreator().showSummon(player->getID(), puppet, false));
	}
	else {
		if(main)
			player->getMap()->send(PacketCreator().showSummon(player->getID(), main, false));
		if(puppet)
			player->getMap()->send(PacketCreator().showSummon(player->getID(), puppet, false));
	}
}

int Summon::ids = 0x300;